#!/usr/bin/perl

use strict;
use Posix;
use Time::Local;

die "usage: survival_DDQB_to_Rtabular.pl DDQB_tab_delim_file> tab_delimited_file_with_header_for_R" unless ($#ARGV==0);

my $sep="\t";
my $file = $ARGV[0];


my @Headers=(
# required headers
"SUBJECTID","ALIVESTATUS",
"DAYSTOBIRTH","BIRTHDATE",	"YEAROFBIRTH","DAYOFBIRTH","MONTHOFBIRTH",
"DAYSTODEATH","DEATHDATE","YEAROFDEATH","DAYOFDEATH","MONTHOFDEATH",
"DAYSTOLASTFOLLOWUP","DAYSTOLASTKNOWNALIVE","LASTFOLLOWUPDATE","DATEOFLASTFOLLOWUP","YEAROFLASTFOLLOWUP","DAYOFLASTFOLLOWUP","MONTHOFLASTFOLLOWUP",
"DATEOFINITIALPATHOLOGICALDIAGNOSTIC","DIAGNOSISDATE","YEAROFINITIALPATHDX","DAYOFINITIALPATHDX","MONTHOFINITIALPATHDX",
"GENDER",
"AGEATINITIALPATHOLOGICALDIAGNOSIS", "DIAGNOSISDATE",# IN YEARS
#DAYS ARE FROM DIAGNOSTIC DATE
"DAYSTOTUMORPROGRESSION","DATEOFTUMORPROGRESSION","YEAROFTUMORPROGRESSION","DAYOFTUMORPROGRESSION","MONTHOFTUMORPROGRESSION",
"DAYSTOTUMORRECURRENCE","DATEOFTUMORRECURRENCE","YEAROFTUMORRECURRENCE","DAYOFTUMORRECURRENCE","MONTHOFTUMORRECURRENCE",
"DAYSTOINITIALPATHOLOGICALDIAGNOSIS","DAYSTOINITIALPATHOLOGICDIAGNOSIS","DAYSTOTUMORRECURRENCE",

"SUBJECTSITENAME",
"TUMORSTAGE"

);
my @hindex=();
my %HEAD2POS;
for(my $i=0;$i<=$#Headers;$i++) {
	push(@hindex,-1);
	my $chead = $Headers[$i];
	$chead =~ tr/a-z/A-Z/;
	$HEAD2POS{$chead}=$i;
}
open FIN,"<$file";
my $l=<FIN>;
chomp $l;
$l =~ s/[\t,\r,\n,\l]+$//g; # also remove trailing tabs
my @line = split($sep,$l);
for(my $i=0;$i<=$#line;$i++) {
	my $chead =$line[$i];
	$chead =~ tr/a-z/A-Z/;
	$chead =~ s/[ ,_]//g;
	if(exists $HEAD2POS{$chead}) {$hindex[$HEAD2POS{$chead}]=$i;}  
}
for(my $i=0;$i<=$#hindex;$i++) {if($hindex[$i]==-1) {print STDERR "Warning: Missing field " . $Headers[$i] . "\n";}}



if(($hindex[0]==-1) && ($hindex[0]==-1 && $hindex[0]==-1)) {die "missing sample ID header. Need that\n";}

print STDERR "WARNING:WARNING: The status at pfs and dfs has not yet been coded!\n";

print "SUBJECT\tAGE\tGENDER\tOS\tPFS\tDFS\tALIVEATOS\tALIVEATPFS\tALIVEATDFS\tSITE\n";

while($l=<FIN>) {
	chomp $l;
	$l =~ s/[\r,\n,\l]+$//g;
	my $skipline=0;
	my @line = split($sep,$l);
	my $subject = $line[$hindex[0]];
	if(length($subject)==0) {
		next;
	}
	my $gender="";
	my $alive_status = "";
	if($hindex[$HEAD2POS{"ALIVESTATUS"}]!=-1) { 
		$alive_status = $line[$hindex[$HEAD2POS{"ALIVESTATUS"}]];
		$alive_status =~ tr/a-z/A-Z/;
		if($alive_status eq "ALIVE" || $alive_status eq "LIVING" || $alive_status eq "YES") {
			$alive_status="1";
		} elsif($alive_status eq "DEAD" || $alive_status eq "DECEASED" || $alive_status eq "NO") {
			$alive_status = "0";
		} else {
			$alive_status = "";
		}
	}
	
	my $birthdateSTR="";
	my $birthmonthSTR="";
	my $birthdaySTR="";
	my $birthyearSTR="";
	if($hindex[$HEAD2POS{"BIRTHDATE"}]!=-1) { 
		$birthdateSTR = $line[$hindex[$HEAD2POS{"BIRTHDATE"}]];
	}
	if($hindex[$HEAD2POS{"YEAROFBIRTH"}]!=-1) { 
		$birthyearSTR = $line[$hindex[$HEAD2POS{"YEAROFBIRTH"}]];
	}

	if($hindex[$HEAD2POS{"MONTHOFBIRTH"}]!=-1) { 
		$birthmonthSTR = $line[$hindex[$HEAD2POS{"MONTHOFBIRTH"}]];
	}
	if($hindex[$HEAD2POS{"DAYOFBIRTH"}]!=-1) { 
		$birthdaySTR = $line[$hindex[$HEAD2POS{"DAYOFBIRTH"}]];
	}
	my $birthdatetime = processDates($birthdateSTR,$birthmonthSTR,$birthdaySTR,$birthyearSTR,1);
	
	
	my $lastfollowupdateSTR="";
	my $lastfollowupmonthSTR="";
	my $lastfollowupdaySTR="";
	my $lastfollowupyearSTR="";
	if($hindex[$HEAD2POS{"LASTFOLLOWUPDATE"}]!=-1) { 
		my $this_lastfollowupdateSTR = $line[$hindex[$HEAD2POS{"LASTFOLLOWUPDATE"}]];
		$this_lastfollowupdateSTR =~ tr/a-z/A-Z/;
		if(length($this_lastfollowupdateSTR)>0 && !($this_lastfollowupdateSTR eq "NULL")) {
			$lastfollowupdateSTR =$this_lastfollowupdateSTR ;
		}
		
	}
	if($hindex[$HEAD2POS{"DATEOFLASTFOLLOWUP"}]!=-1) { 
		my $this_lastfollowupdateSTR = $line[$hindex[$HEAD2POS{"LASTFOLLOWUPDATE"}]];
		$this_lastfollowupdateSTR =~ tr/a-z/A-Z/;
		if(length($this_lastfollowupdateSTR)>0 && !($this_lastfollowupdateSTR eq "NULL")) {
			$lastfollowupdateSTR =$this_lastfollowupdateSTR ;
		}
	}
	
	if($hindex[$HEAD2POS{"YEAROFLASTFOLLOWUP"}]!=-1) { 
		$lastfollowupyearSTR = $line[$hindex[$HEAD2POS{"YEAROFLASTFOLLOWUP"}]];
	}

	if($hindex[$HEAD2POS{"MONTHOFLASTFOLLOWUP"}]!=-1) { 
		$lastfollowupmonthSTR = $line[$hindex[$HEAD2POS{"MONTHOFLASTFOLLOWUP"}]];
	}
	if($hindex[$HEAD2POS{"DAYOFLASTFOLLOWUP"}]!=-1) { 
		$lastfollowupdaySTR = $line[$hindex[$HEAD2POS{"DAYOFLASTFOLLOWUP"}]];
	}
	my $lastfollowupdatetime = processDates($lastfollowupdateSTR,$lastfollowupmonthSTR,$lastfollowupdaySTR,$lastfollowupyearSTR,0);

	
	# diag date
	my $diagnosticdateSTR="";
	my $diagnosticmonthSTR="";
	my $diagnosticdaySTR="";
	my $diagnosticyearSTR="";
	if($hindex[$HEAD2POS{"DATEOFINITIALPATHOLOGICALDIAGNOSTIC"}]!=-1) { 
		$diagnosticdateSTR = $line[$hindex[$HEAD2POS{"DATEOFINITIALPATHOLOGICALDIAGNOSTIC"}]];
	} 
	if($hindex[$HEAD2POS{"DIAGNOSISDATE"}]!=-1) {
			$diagnosticdateSTR =~ tr/a-z/A-Z/;
			my $this_diag = $line[$hindex[$HEAD2POS{"DIAGNOSISDATE"}]];
			$this_diag =~ tr/a-z/A-Z/;
			if(length($diagnosticdateSTR)==0 || $diagnosticdateSTR eq "NULL" || length($diagnosticdateSTR) < length($this_diag)) {
					$diagnosticdateSTR =	$this_diag;					
			}
	} 
	if((length($diagnosticdateSTR)==0  || $diagnosticdateSTR =~ /[N,n][U,u][L,l][L,l]/) && $hindex[$HEAD2POS{"DATEOFINITIALPATHOLOGICDIAGNOSTIC"}]!=-1) { 
		$diagnosticdateSTR = $line[$hindex[$HEAD2POS{"DATEOFINITIALPATHOLOGICDIAGNOSTIC"}]];
	} 
	if((length($diagnosticdateSTR)==0  || $diagnosticdateSTR =~ /[N,n][U,u][L,l][L,l]/) &&  $hindex[$HEAD2POS{"DIAGNOSISDATE"}]!=-1) { 
		$diagnosticdateSTR = $line[$hindex[$HEAD2POS{"DIAGNOSISDATE"}]];
		print "diag date = $diagnosticdateSTR\n";
	}
	if($hindex[$HEAD2POS{"YEAROFINITIALPATHDX"}]!=-1) { 
		$diagnosticyearSTR = $line[$hindex[$HEAD2POS{"YEAROFINITIALPATHDX"}]];
	}

	if($hindex[$HEAD2POS{"MONTHOFINITIALPATHDX"}]!=-1) { 
		$diagnosticmonthSTR = $line[$hindex[$HEAD2POS{"MONTHOFINITIALPATHDX"}]];
	}
	if($hindex[$HEAD2POS{"DAYOFINITIALPATHDX"}]!=-1) { 
		$diagnosticdaySTR = $line[$hindex[$HEAD2POS{"DAYOFINITIALPATHDX"}]];
	}
	my $diagnosticdatetime = processDates($diagnosticdateSTR,$diagnosticmonthSTR,$diagnosticdaySTR,$diagnosticyearSTR,0);
	
	
	
	
	# Death Date
	
	my $deathdateSTR="";
	my $deathmonthSTR="";
	my $deathdaySTR="";
	my $deathyearSTR="";
	if($hindex[$HEAD2POS{"DEATHDATE"}]!=-1) { 
		$deathdateSTR = $line[$hindex[$HEAD2POS{"DEATHDATE"}]];
	}
	if($hindex[$HEAD2POS{"YEAROFDEATH"}]!=-1) { 
		$deathyearSTR = $line[$hindex[$HEAD2POS{"YEAROFDEATH"}]];
	}

	if($hindex[$HEAD2POS{"MONTHOFDEATH"}]!=-1) { 
		$deathmonthSTR = $line[$hindex[$HEAD2POS{"MONTHOFDEATH"}]];
	}
	if($hindex[$HEAD2POS{"DAYOFDEATH"}]!=-1) { 
		$deathdaySTR = $line[$hindex[$HEAD2POS{"DAYOFDEATH"}]];
	}
	my $deathdatetime = processDates($deathdateSTR,$deathmonthSTR,$deathdaySTR,$deathyearSTR,0);


	# check that death>birth
	if($deathdatetime !=-1 && $birthdatetime!=-1) {
		if($deathdatetime<$birthdatetime) {
			$skipline=1;
				
			print STDERR "QCERROR: Skipping sample $subject, because death($deathdatetime) occured before birth($birthdatetime)\n";
		}
		
	}
	
	# If have alive_status = dead, but no death date == .. 
	if($deathdatetime ==-1 && $alive_status ==0) {
		 $deathdatetime = processDates($deathdateSTR,$deathmonthSTR,$deathdaySTR,$deathyearSTR,15);
			
	}
	
	
	my $recurrencedateSTR="";
	my $recurrencemonthSTR="";
	my $recurrencedaySTR="";
	my $recurrenceyearSTR="";
	if($hindex[$HEAD2POS{"DATEOFTUMORRECURRENCE"}]!=-1) { 
		$recurrencedateSTR = $line[$hindex[$HEAD2POS{"DATEOFTUMORRECURRENCE"}]];
	}
	if($hindex[$HEAD2POS{"YEAROFTUMORRECURRENCE"}]!=-1) { 
		$recurrenceyearSTR = $line[$hindex[$HEAD2POS{"YEAROFTUMORRECURRENCE"}]];
	}

	if($hindex[$HEAD2POS{"MONTHOFTUMORRECURRENCE"}]!=-1) { 
		$recurrencemonthSTR = $line[$hindex[$HEAD2POS{"MONTHOFTUMORRECURRENCE"}]];
	}
	if($hindex[$HEAD2POS{"DAYOFTUMORRECURRENCE"}]!=-1) { 
		$recurrencedaySTR = $line[$hindex[$HEAD2POS{"DAYOFTUMORRECURRENCE"}]];
	}
	my $recurrencedatetime = processDates($recurrencedateSTR,$recurrencemonthSTR,$recurrencedaySTR,$recurrenceyearSTR,0);
	
	
	
	my $progressiondateSTR="";
	my $progressionmonthSTR="";
	my $progressiondaySTR="";
	my $progressionyearSTR="";
	if($hindex[$HEAD2POS{"DATEOFTUMORPROGRESSION"}]!=-1) { 
		$progressiondateSTR = $line[$hindex[$HEAD2POS{"DATEOFTUMORPROGRESSION"}]];
	}
	if($hindex[$HEAD2POS{"YEAROFTUMORPROGRESSION"}]!=-1) { 
		$progressionyearSTR = $line[$hindex[$HEAD2POS{"YEAROFTUMORPROGRESSION"}]];
	}

	if($hindex[$HEAD2POS{"MONTHOFTUMORPROGRESSION"}]!=-1) { 
		$progressionmonthSTR = $line[$hindex[$HEAD2POS{"MONTHOFTUMORPROGRESSION"}]];
	}
	if($hindex[$HEAD2POS{"DAYOFTUMORPROGRESSION"}]!=-1) { 
		$progressiondaySTR = $line[$hindex[$HEAD2POS{"DAYOFTUMORPROGRESSION"}]];
	}
	my $progressiondatetime = processDates($progressiondateSTR,$progressionmonthSTR,$progressiondaySTR,$progressionyearSTR,0);

	
	
	
	
	# check that death date > max(lastfollowupdate,recurrence,progression)
	my $maxdate = -1;
	if($lastfollowupdatetime!=-1) {
		$maxdate = $lastfollowupdatetime;
	}
	if($recurrencedatetime!=-1 && $recurrencedatetime>$maxdate) {
		$maxdate = $recurrencedatetime;
	}
	if($progressiondatetime!=-1  && $progressiondatetime>$maxdate) {
		$maxdate = $progressiondatetime;
	}
			
	if($deathdatetime !=-1) {
		if($deathdatetime<$maxdate) {
				$maxdate=$deathdatetime;
		}			
	}
	# check that lastfollowup>diagnostic date
	if($diagnosticdatetime!=-1 && $lastfollowupdatetime!=-1) {
		if($diagnosticdatetime > $lastfollowupdatetime) {
			$skipline=1;
			print STDERR "QCERROR: Skipping sample $subject, because diagnostic date occured after last followup date\n";
		}
	}		
	
	# If have death date, set alivestatus
	if($deathdatetime!=-1) {
		$alive_status=0;
	}
	my $gender="";
	if($hindex[$HEAD2POS{"GENDER"}]!=-1) { 
		$gender = $line[$hindex[$HEAD2POS{"GENDER"}]];
		$gender =~ tr/a-z/A-Z/;
		if($gender eq "MALE" || $gender eq "M") {
			$gender="M";
		} elsif ($gender eq "FEMALE" || $gender eq "F") {
			$gender = "F";
		} else {
			$gender = "";
		}
	}
	
	

	my $site = "";
	if($hindex[$HEAD2POS{"SUBJECTSITENAME"}]!=-1) { 
		$site = $line[$hindex[$HEAD2POS{"SUBJECTSITENAME"}]];
		$site =~ tr/a-z/A-Z/;
		$site =~ s/[ ,_]//g;
		
	}
	
	
	# Compute overall survival time (from diagnostic date)
	my $os=-1;
	my $pfs=-1; #time to progression or recurrence of time to last followup (smallest)
	my $dfs=-1;#disease-free survival. Time to recurrence, death, or last followup, exclude points with progression dates
	my $pfs_status; # status at progression time (0 - no disease (e.g. last followup) or 1 diseased/dead
	my $dfs_status; # status at disease free time (0 - no disease (e.g. last followup) or 1 diseased/dead)

	if($hindex[$HEAD2POS{"DAYSTOTUMORPROGRESSION"}]!=-1) { 
			my $this_pfs = $line[$hindex[$HEAD2POS{"DAYSTOTUMORPROGRESSION"}]];
			$this_pfs =~ tr/a-z/A-Z/;
			if(length($this_pfs)>0 && !($this_pfs eq "NULL")) {
				$pfs=$this_pfs;
			}
	}
	if($hindex[$HEAD2POS{"DAYSTOTUMORRECURRENCE"}]!=-1) {
			if($pfs==-1) { # if there was Tumor progression means there was never any cure
				my $this_dfs = $line[$hindex[$HEAD2POS{"DAYSTOTUMORRECURRENCE"}]];
				$this_dfs =~ tr/a-z/A-Z/;
				if(length($this_dfs)>0 && !($this_dfs eq "NULL")) {
					$dfs=$this_dfs;
					if($dfs<$pfs) {$pfs=$dfs;}
				}
			}
	}
	if($hindex[$HEAD2POS{"DAYSTOLASTKNOWNALIVE"}]!=-1) { 
			my $this_os = $line[$hindex[$HEAD2POS{"DAYSTOLASTKNOWNALIVE"}]];
			$this_os =~ tr/a-z/A-Z/;
			if(length($this_os)>0 && !($this_os eq "NULL")) {
				$os=$this_os;
			}
	}
	if($hindex[$HEAD2POS{"DAYSTOLASTFOLLOWUP"}]!=-1) { 
			my $this_os = $line[$hindex[$HEAD2POS{"DAYSTOLASTFOLLOWUP"}]];
			$this_os =~ tr/a-z/A-Z/;
			if(length($this_os)>0 && !($this_os eq "NULL")) {
				if($os==-1 || $this_os>$os) {
					$os=$this_os;
				}
			}
	}
	if($hindex[$HEAD2POS{"DAYSTODEATH"}]!=-1) { 
			my $this_os = $line[$hindex[$HEAD2POS{"DAYSTODEATH"}]];
			$this_os =~ tr/a-z/A-Z/;
			if(length($this_os)>0 && !($this_os eq "NULL")) {
				$os=$this_os;
			}
	}

	if($deathdatetime ==-1 && $os==-1 && ($lastfollowupdatetime==-1)) {
			$skipline=1;
#			print STDERR "lfstr=$lastfollowupdateSTR,lf=$lastfollowupdatetime,dd=$deathdateSTR,$deathdatetime\n";
	
			print STDERR "QCMISSINGDATA: Skipping sample $subject, because person has no final event time (death or last followup)\n"
	}

	my $age=0;
	if($diagnosticdatetime==-1) {
		# leave $os,$pfs, and $dfs as is.
	} else {
		if($deathdatetime!=-1 ) {
			my $this_os = ($deathdatetime-$diagnosticdatetime)/(24*3600);
			if($this_os>$os) {
				$os=$this_os;
			}
		} elsif($os==-1 && $maxdate!=-1) { # no days_to_death
			my $this_os = ($maxdate-$diagnosticdatetime)/(24*3600);
			if($this_os>$os) {
				$os=$this_os;
			}
		}

		
		# Compute time status at progresssion free survival time (alive or dead .. missing is same as alive)
		if($progressiondatetime!=-1 ) {
			my $this_pfs = ($progressiondatetime-$diagnosticdatetime)/(24*3600);
			if($pfs==-1) {
				$pfs=$this_pfs;
			}
		}
		if($recurrencedatetime!=-1 ) {
			my $this_dfs =  ($recurrencedatetime-$diagnosticdatetime)/(24*3600);
			if($dfs==-1 || ($dfs>0 && $this_dfs<$dfs)) {
				$dfs = $this_dfs;
			}
			if($pfs!=-1) {
				$dfs=-1; #Cannot have ever been disease free is $pfs exists
			}
		}

		# compute age @ diagnostic or surgery data
		
			
		#XXX Make sure Pfs and rfs times are not negative
		
		# If want dfs (disease free survival ==> could compute from surgery date)
		# samples with a progression date are disqualified... this assumes the cancer was never cured.
		
	}
	
	# Progression-Free survival could be from surgery date OR diagnostic.. we are doing the latter.
	# The earliest of recurrence, progression, or death is an event
	my $age_from_file = -1;
	if($hindex[$HEAD2POS{"AGEATINITIALPATHOLOGICALDIAGNOSIS"}]!=-1) { 
		$age_from_file = $line[$hindex[$HEAD2POS{"AGEATINITIALPATHOLOGICALDIAGNOSIS"}]];
		$age_from_file =~ tr/a-z/A-Z/;
		if($age_from_file =~ /NULL/) {
			$age_from_file=0;
		}
	}
	my $computed_age =-1;
	if(($birthdatetime!=-1) && ($diagnosticdatetime!=-1)) {
		$computed_age = ($diagnosticdatetime-$birthdatetime)/(365.25*24*3600);
		#print STDERR "$computed_age\n";
	}
	if($computed_age!=-1 && $age_from_file!=-1) {
		if(abs($age_from_file-$computed_age)>2) {
			print STDERR "QCWARNING for $subject, age_fromfile $age_from_file != $computed_age (computed_age)\n"; 
		}
	}
	if($computed_age==-1 && $age_from_file>0) {
		$computed_age=$age_from_file;
	#	print STDERR "$computed_age\n";
	}
	if($computed_age==-1) {
		$computed_age="?";
	}
	
	# Disease-free survival ==> only recurrence counts (excludes patients with progression data.. which indicates they were not cured)
	
	
	if($skipline==0) {
		if($os==-1) {$os="";} else {$os=sprintf("%.2f",$os/(365/12));} # convert to months
		if($pfs==-1) {$pfs="";}else {$pfs=sprintf("%.2f",$pfs/(365/12));}# convert to months
		if($dfs==-1) {$dfs="";}else {$dfs=sprintf("%.2f",$dfs/(365/12));}# convert to months
		if(length($alive_status)>0 && !($alive_status eq "null" || $alive_status eq "NULL")) {
			$alive_status = 1-$alive_status; #switch from alive=1 to dead =1
		}
		if(length($pfs_status)>0 && !($pfs_status eq "null" || $pfs_status eq "NULL")) {
					$pfs_status = 1-$pfs_status;
		}
		if(length($dfs_status)>0 && !($dfs_status eq "null" || $dfs_status eq "NULL")) {
					$dfs_status = 1-$dfs_status;
		}
		if($computed_age==-1) {$computed_age="";} else {
			$computed_age = sprintf("%.0f",$computed_age);
		}
		print "$subject\t$computed_age\t$gender\t$os\t$pfs\t$dfs\t$alive_status\t$pfs_status\t$dfs_status\t$site\n";

	}
}

# parameters: whole date
# month of the date 0-based (already given as such in TCGA!!)
# day of the date
# year of the date
# default_day
# 
sub processDates() {
# process dates,
	my ($maindateSTR,$monthSTR,$daySTR,$yearSTR,$default_day) = @_;
	my $datetime = -1; #invalid datetime.. I hope.
	$maindateSTR =~ tr/a-z/A-Z/;
	my $y=-1;
	my $m=-1;
	my $d=-1;
	if(length($maindateSTR)>0 && ! ($maindateSTR eq "NULL")) {
		if($maindateSTR =~ /^([0-9]+)-([0-9]+)-([0-9]+)$/) { #2002-12-20
			$y=$1;
			$m=$2;
			$d=$3;
			$datetime = timelocal(0,0,0,$d,$m-1,$y); 
		} elsif ($maindateSTR =~ /^([0-9]+)\/([0-9]+)\/([0-9]+)$/) { #12/20/2002
			$y=$3;
			$m=$1;
			$d=$2;
			$datetime = timelocal(0,0,0,$d,$m-1,$y); 
		} 

		if($datetime!=-1) {
			return $datetime;
		}
	}
	my $alt_datetime =-1;

	$monthSTR =~ tr/a-z/A-Z/;
	$daySTR =~ tr/a-z/A-Z/;
	$yearSTR =~ tr/a-z/A-Z/;
	if(length($maindateSTR)==0 || $maindateSTR =~ /NULL/ ) {
		if(!(length($yearSTR)==0 || $yearSTR =~ /NULL/)) {
			if(!(length($monthSTR)==0 || $monthSTR =~ /NULL/)) {
				if(length($daySTR)==0 || $daySTR =~ /NULL/) {
					if($default_day>0) {
						$alt_datetime=timelocal(0,0,0,$default_day,$monthSTR, $yearSTR); 
					} 
				}  else {
					$alt_datetime=timelocal(0,0,0,$daySTR,$monthSTR, $yearSTR);
					if($datetime!=-1 && $alt_datetime!=-1 && $datetime!=$alt_datetime) {
						print "WARNING: $maindateSTR != $monthSTR/$daySTR/$yearSTR\n";
					}

				}
			}
		}
	}
	if($datetime!=-1) {
		return $datetime;
	} else {
		return $alt_datetime;
	}
}
